package com.ruoyi.event.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.event.domain.bo.EventLeaveBo;
import com.ruoyi.event.domain.vo.EventLeaveVo;
import com.ruoyi.event.service.IEventLeaveService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 请假
 *
 * @author strongme
 * @date 2023-02-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/event/leave")
public class EventLeaveController extends BaseController {

    private final IEventLeaveService iEventLeaveService;

    /**
     * 查询请假列表
     */
    @GetMapping("/list")
    public TableDataInfo<EventLeaveVo> list(EventLeaveBo bo, PageQuery pageQuery) {
        return iEventLeaveService.queryPageList(bo, pageQuery);
    }

    @PostMapping("query")
    public R<List<EventLeaveVo>> query(@RequestBody EventLeaveBo bo) {
        return R.ok(iEventLeaveService.queryList(bo));
    }

    /**
     * 导出请假列表
     */
    @Log(title = "请假", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EventLeaveBo bo, HttpServletResponse response) {
        List<EventLeaveVo> list = iEventLeaveService.queryList(bo);
        ExcelUtil.exportExcel(list, "请假", EventLeaveVo.class, response);
    }

    /**
     * 获取请假详细信息
     *
     * @param leaveId 主键
     */
    @GetMapping("/{leaveId}")
    public R<EventLeaveVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long leaveId) {
        return R.ok(iEventLeaveService.queryById(leaveId));
    }

    /**
     * 新增请假
     */
    @Log(title = "请假", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EventLeaveBo bo) {
        return toAjax(iEventLeaveService.insertByBo(bo));
    }

    /**
     * 修改请假
     */
    @Log(title = "请假", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EventLeaveBo bo) {
        return toAjax(iEventLeaveService.updateByBo(bo));
    }

    /**
     * 删除请假
     *
     * @param leaveIds 主键串
     */
    @Log(title = "请假", businessType = BusinessType.DELETE)
    @DeleteMapping("/{leaveIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] leaveIds) {
        return toAjax(iEventLeaveService.deleteWithValidByIds(Arrays.asList(leaveIds), true));
    }
}
